package com.geeklan.smart.ui.pet.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geeklan.smart.R;

import java.util.List;

public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ADD = 0;  // 添加图片布局
    private static final int TYPE_PHOTO = 1; // 显示图片布局

    private final Context context;
    private final List<String> photoList; // 图片路径列表
    private final int maxPhotos = 10; // 最大图片数量
    private final PhotoAdapterListener listener;

    public interface PhotoAdapterListener {
        void onAddPhotoClick(); // 添加图片按钮点击回调
        void onDeletePhotoClick(int position); // 删除图片回调
    }

    public PhotoAdapter(Context context, List<String> photoList, PhotoAdapterListener listener) {
        this.context = context;
        this.photoList = photoList;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        // 如果是第一个位置且图片未满10张，则显示添加图片布局
        if (position == 0 && photoList.size() < maxPhotos) {
            return TYPE_ADD;
        } else {
            return TYPE_PHOTO;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ADD) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_add_photo, parent, false);
            return new AddPhotoViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_photo, parent, false);
            return new PhotoViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AddPhotoViewHolder) {
            // 添加图片逻辑
            AddPhotoViewHolder addHolder = (AddPhotoViewHolder) holder;
            addHolder.itemView.setOnClickListener(v -> listener.onAddPhotoClick());
        } else if (holder instanceof PhotoViewHolder) {
            // 显示图片逻辑
            PhotoViewHolder photoHolder = (PhotoViewHolder) holder;
            String photoPath = photoList.get(position - (photoList.size() < maxPhotos ? 1 : 0)); // 计算图片索引
            Glide.with(context).load(photoPath).into(photoHolder.imageViewPhoto);

            photoHolder.itemView.setOnClickListener(v -> listener.onDeletePhotoClick(position));
        }
    }

    @Override
    public int getItemCount() {
        // 如果未满10张，加上添加按钮，否则只显示图片
        return photoList.size() < maxPhotos ? photoList.size() + 1 : photoList.size();
    }

    public static class AddPhotoViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewAddPhoto;

        public AddPhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewAddPhoto = itemView.findViewById(R.id.imageViewAddPhoto);
        }
    }

    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewPhoto, imageViewDelete;

        public PhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewPhoto = itemView.findViewById(R.id.imageViewPhoto);
            imageViewDelete = itemView.findViewById(R.id.imageViewDelete);
        }
    }
}
